<template>
  <div>
    <div class="tilteWrap">
      <em class="title">商品管理</em>
      <img src="@/assets/point.png" alt="" class="point" @click="show = true" />
    </div>
    <div class="wrap">
      <div class="titleWarp">
        <div
          class="titleItem"
          :class="{ titleItemActiv: typeId == -1 }"
          @click="reqList(-1)"
        >
          推荐
        </div>
        <div
          class="titleItem"
          v-for="(item, index) in resShopTypeList"
          :key="index"
          :class="{ titleItemActiv: typeId == item.id }"
          @click="reqList(item.id)"
        >
          {{ item.title }}
        </div>
      </div>
      <div class="filterWrap">
        <div class="filterItem" @click="orderByPrice">
          价格
          <img src="@/assets/paixu2.png" alt="" />
        </div>
        <div class="filterItem filterItemLine"></div>
        <div class="filterItem" @click="orderBy()">
          销量
          <img src="@/assets/paixu2.png" alt="" />
        </div>
      </div>
      <div class="productListWrap" v-if="resGoodsList.length > 0">
        <div class="productItem" v-for="(item, index) in resGoodsList" :key="index" @click="jumpGoodsInfo(item.id)">
          <img :src="$imgUrl + item.listImgUrl" alt="" class="proPosterLeft" />
          <div class="proIntroRight2">
            <p class="proTitle2">{{ item.goodsName }}</p>
            <p class="saleNum2">已售{{ item.saleNum }}</p>
            <div class="priceWrap2">
              <em>￥</em>{{ item.price }}
            </div>
          </div>
        </div>
      </div>
      <div class="productListWrap productListWrap2" v-if="resGoodsList.length == 0">
        该类型暂无商品，切换类型试试或点击右上角按钮添加对应类型商品吧~
      </div>
    </div>
    <!-- 遮罩层-举报、删除、回复： -->
    <div class="maskWrap" v-if="show">
      <div class="maskBg" @click="show = false"></div>
      <div class="chooseNumWrap">
        <div class="optionsItem" @click="jumpAddGoods">新增商品</div>
        <div class="optionsItem" @click="jumpSortSet">分类设置</div>
        <div class="optionsItem" @click="jumpSetRecommend">推荐设置</div>
        <div class="optionsItem dele" @click="show = false">取消</div>
      </div>
    </div>
    <!-- <tabbar v-if="!show"></tabbar> -->
  </div>
</template>

<script>
// import Axios from 'axios'
import { Search } from "vant";
import {
  apiGetGoodsTypeList,
  apiGetGoodsList,
  apiPlatformGetUserShopDetails,
} from "@/request/api"; // api接口
import tabbar from "../components/tabbarB";
export default {
  name: "App",
  data() {
    return {
      show: false,
      shopId: "", //商铺id
      resGoodsList: [], //商品列表
      resShopTypeList: "",
      reqpageSize: 5, //分页条数
      reqPageNum: 0, //当前第几页
      noMore: false,
      isAsc: "desc", //排序
      typeId: -1, //推荐类型为-1.其余为类型id
      orderByWay: "price", //销量-saleNum，价格-price
      // _____________
    };
  },
  created() {},
  mounted() {
      localStorage.removeItem("Information") //清除添加商品页的商品数据
      localStorage.removeItem("shopGoodsSpecsList") //清除添加商品页的商品规格数据
      this.getShopDetails();
  },
  methods: {
    //分页 监听滑动事件
    handleScroll() {
      let Top = $(window).scrollTop();
      let WinH = $(window).height();
      let DocH = $(document).height();
      if (DocH - (Top + WinH) <= 0) {
        if (this.noMore) {
          return false;
        }
        this.noMore = false;
        this.getPlatformGoodsList();
      }
    },
    //店铺详情api获取商铺id：
    getShopDetails() {
      apiPlatformGetUserShopDetails({}).then((res) => {
        this.shopId = res.data.id;
        this.getPlatformGoodsList(); //获取首次商品列表
        this.platformTypeList(); //通过商铺id获取店铺商品类型
        window.addEventListener("scroll", this.handleScroll, true);
      });
    },
    //获取商品类型：
    platformTypeList() {
      apiGetGoodsTypeList({
        id: this.shopId,
      }).then((res) => {
        this.resShopTypeList = res.data;
        console.log("商品类型:", this.resShopTypeList);
      });
    },
    //获取商品列表：
    getPlatformGoodsList() {
      var page = (this.reqPageNum += 1);
      console.log('page',page)
      apiGetGoodsList({
        shopId: this.shopId,
        typeId: this.typeId,
        pageNum: page,
        pageSize: this.reqpageSize,
        orderByColumn: this.orderByWay,
        isAsc: this.isAsc,
      }).then((res) => {
        if (res.data.rows.length == 0) {
          this.noMore = true;
          return;
        }
         window.addEventListener("scroll", this.handleScroll, true);
        var dataRes = res.data.rows;
        this.resGoodsList = [...this.resGoodsList, ...dataRes];
        console.log("resGoodsList-推荐", this.resGoodsList);
      });
    },
    reqList(i) {
      this.noMore = false;
      this.reqPageNum = 0;
      this.resGoodsList = [];
      this.typeId = i;
      this.getPlatformGoodsList();
    },
    //切换销量：
    orderBy() {
      this.orderByWay = "saleNum";
      this.isAsc = this.isAsc == "desc" ? "asc" : "desc";
      this.reqPageNum = 0;
      this.resGoodsList = [];
      this.getPlatformGoodsList();
    },
    //切换价格
    orderByPrice() {
      this.orderByWay = "price";
      this.isAsc = this.isAsc == "desc" ? "asc" : "desc";
      this.reqPageNum = 0;
      this.resGoodsList = [];
      this.getPlatformGoodsList();
    },
    //商品详情页;
    jumpGoodsInfo(id) {
      this.$router.push({
        path: "/pages/bMyGoodsInfo",
        query: {
          //传递参数
          GoodsId:id
        },
      });
    },
    //新增商品页：
    jumpAddGoods() {
      this.$router.push({
        path: "/pages/bAddGoods",
        query: {
          //传递参数
        },
      });
    },
   //分类设置
    jumpSortSet() {
      this.$router.push({
        path: "/pages/sortSet",
        query: {
          //传递参数
        },
      });
    },
    //推荐设置
    jumpSetRecommend() {
      this.$router.push({
        path: "/pages/bSetRecommend",
        query: {
          //传递参数
        },
      });
    },
    
  },
  components: {
    tabbar,
    [Search.name]: Search,
  },
};
</script>

<style scoped>
.tilteWrap {
  height: 50px;
  line-height: 50px;
}
.title {
  font-size: 16px;
  font-weight: 500;
  color: #181818;
  text-align: center;
  float: left;
  margin-left: 40%;
}
.point {
  float: right;
  display: inline-block;
  width: 13px;
  height: 3px;
  margin-top: 23px;
  margin-right: 20px;
}
.wrap {
  background-color: #fff;
  padding: 0px 15px;
  padding-top: 2px;
  /* min-height:50vh; */
  border-radius: 30px 30px 0px 0px;
}
.titleWarp {
  width: calc(100% - 30px);
  margin: 20px auto;
  height: 35px;
  line-height: 30px;
  overflow-y: hidden;
  overflow-x: scroll;
  display: -webkit-box;
}
.titleItem {
  font-size: 15px;
  font-weight: 500;
  color: #181818;
  margin-right: 30px;
  /* overflow: hidden; */
  /* float: left; */
}
.titleItemActiv {
  font-size: 16px;
  font-weight: 500;
  color: #181818;
  border-bottom: 4px solid #000;
}
.filterWrap {
  width: calc(100% - 26px);
  height: 34px;
  line-height: 34px;
  background: #181818;
  border-radius: 17px;
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  padding-left: 26px;
  margin-top: 10px;
}
.filterItem {
  float: left;
  margin-right: 15px;
}
.filterItem > img {
  width: 5px;
  height: 5px;
  margin-bottom: 2px;
}
.filterItemLine {
  height: 15px;
  width: 1px;
  background-color: #666666;
  margin-top: 10px;
}
.maskWrap {
}
.maskBg {
  width: 100%;
  min-height: 100vh;
  opacity: 0.35;
  background-color: #000;
  position: fixed;
  top: 0px;
}
.chooseNumWrap {
  width: 100%;
  background-color: #fff;
  position: fixed;
  bottom: 0px;
  border-radius: 20px 20px 0px 0px;
}
.inputComment {
  height: 100px;
}
.inputComment .van-cell {
  height: 80px !important;
}
.optionsItem {
  height: 50px;
  line-height: 50px;
  border-bottom: 1px solid #eee;
  color: #000;
  font-size: 14px;
  text-align: center;
}
.dele {
  background-color: #eee;
  border-radius: 20px;
  width: 90%;
  margin: 5px auto;
  height: 40px;
  line-height: 40px;
}
.productListWrap {
  width: calc(100% - 30px);
  margin: 20px auto;
  min-height: 78vh;
}
.productListWrap2 {
  width: 80%;
  margin: 0 auto;
  font-size: 12px;
  color: #fa6a22;
  font-weight: 600;
  text-align: center;
  margin-top: 150px;
  line-height: 25px;
}
.productItem {
  overflow: hidden;
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
.proPosterLeft {
  width: 24%;
  height: 75px;
  border-radius: 15px;
}
.proIntroRight2 {
  width: 70%;
}
.proTitle2 {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 0px;
}
.saleNum2 {
  margin-top: 9px;
  font-size: 10px;
  font-weight: 300;
  color: #666666;
  text-align: left;
  margin-bottom: 0px;
}
.priceWrap2 {
  margin-top: 4px;
  text-align: right;
  font-size: 15px;
  font-weight: bold;
  color: #ff4646;
}
.priceWrap2 em{
  font-size: 10px;
}
</style>
